﻿using System.Configuration;

namespace MyExeSetting
{
    public class MyExeSettingHelper
    {
        #region 单例模式

        /// <summary>
        /// 私有构造，杜绝直接new类
        /// </summary>
        private static readonly object Obj = new object();

        /// <summary>
        /// 私有成员，使用时分配内存 
        /// </summary>
        private static MyExeSettingHelper _singInstance;

        /// <summary>
        /// 获取实例
        /// </summary>
        public static MyExeSettingHelper Instance
        {
            get
            {
                if (_singInstance == null)
                {
                    lock (Obj)
                    {
                        if (_singInstance == null)
                        {
                            _singInstance = new MyExeSettingHelper();
                        }
                    }
                }
                return _singInstance;
            }
        }

        /// <summary>
        /// 初始化实例
        /// </summary>
        public MyExeSettingHelper()
        {
        }

        #endregion 单例模式

        #region 获取系统缓存

        /// <summary>
        /// 软件是否已经关闭 true开启, false关闭
        /// </summary>
        public bool IsVisibile { get; set; }

        /// <summary>
        /// 软件是否可用 true可用, false不可用
        /// </summary>
        public bool IsUsable { get; set; }

        /// <summary>
        /// 平台是否连接 true连接，false未连接
        /// </summary>
        public bool IsPlatform { get; set; }

        #endregion 获取系统缓存

        #region 界面信息配置

        /// <summary>
        /// 软件名称
        /// </summary>
        public string SoftName
        {
            get
            {
                return Read("SoftName");
            }
            set
            {
                Save("SoftName", value);
            }
        }

        /// <summary>
        /// 程序当前版本
        /// </summary>
        public string Version
        {
            get
            {
                return Read("Version");
            }
            set
            {
                Save("Version", value);
            }
        }

        /// <summary>
        /// 程序出版者
        /// </summary>
        public string Author
        {
            get
            {
                return Read("Author");
            }
        }

        #endregion 界面信息配置

        #region 密码

        /// <summary>
        /// 密码,以密文形式存储
        /// </summary>
        public string Password
        {
            get
            {
                string strPassword = Read("Password");
                return strPassword;
            }
            set
            {
                string pwd = string.IsNullOrEmpty(value) ? string.Empty : value;
                Save("Password", pwd);
            }
        }

        /// <summary>
        /// 是否允许跳过密码输入
        /// </summary>
        public bool IsPassword
        {
            get
            {
                string str = Read("IsPassword");
                if (string.IsNullOrEmpty(str) || str == "0")
                    return false;
                return true;
            }
            set
            {
                Save("IsPassword", value ? "1" : "0");
            }
        }

        #endregion 密码

        #region 采集模块

        /// <summary>
        /// 采集模块宽
        /// </summary>
        public double DeviceWidth
        {
            get
            {
                string str = Read("DeviceWidth");
                if (string.IsNullOrEmpty(str))
                    return 0;
                return Convert.ToDouble(str);
            }
            set
            {
                Save("DeviceWidth", value.ToString());
            }
        }

        /// <summary>
        /// 采集模块高
        /// </summary>
        public double DeviceHeight
        {
            get
            {
                string str = Read("DeviceHeight");
                if (string.IsNullOrEmpty(str))
                    return 0;
                return Convert.ToDouble(str);
            }
            set
            {
                Save("DeviceHeight", value.ToString());
            }
        }

        /// <summary>
        /// 采集模块 总数量
        /// </summary>
        public int DeviceCount
        {
            get
            {
                string str = Read("DeviceCount");
                if (string.IsNullOrEmpty(str))
                    return 0;
                return Convert.ToInt32(str);
            }
            set
            {
                Save("DeviceCount", value.ToString());
            }
        }

        /// <summary>
        /// 采集数量
        /// </summary>
        public int CollectCount
        {
            get
            {
                string str = Read("CollectCount");
                if (string.IsNullOrEmpty(str))
                    return 0;
                return Convert.ToInt32(str);
            }
            set
            {
                Save("CollectCount", value.ToString());
            }
        }

        #endregion 采集模块

        #region 采集站文件配置

        /// <summary>
        /// 保存路径集合
        /// </summary>
        public string Directories
        {
            get
            {
                return Read("Directories");
            }
            set
            {
                Save("Directories", value);
            }
        }

        /// <summary>
        /// 视频格式
        /// </summary>
        public string Videos
        {
            get
            {
                return Read("Videos");
            }
            set
            {
                Save("Videos", value);
            }
        }

        /// <summary>
        /// 音频格式
        /// </summary>
        public string Audios
        {
            get
            {
                return Read("Audios");
            }
            set
            {
                Save("Audios", value);
            }
        }

        /// <summary>
        /// 图片格式
        /// </summary>
        public string Images
        {
            get
            {
                return Read("Images");
            }
            set
            {
                Save("Images", value);
            }
        }

        /// <summary>
        /// 日志格式
        /// </summary>
        public string Logs
        {
            get
            {
                return Read("Logs");
            }
            set
            {
                Save("Logs", value);
            }
        }

        /// <summary>
        /// 文件保存（天）
        /// </summary>
        public int SaveFileDay
        {
            get
            {
                string str = Read("SaveFileDay");
                if (string.IsNullOrEmpty(str))
                    return 0;
                return Convert.ToInt32(str);
            }
            set
            {
                Save("SaveFileDay", value.ToString());
            }
        }

        /// <summary>
        /// 是否自动删除记录仪文件
        /// </summary>
        public bool IsAutoDeleteFile
        {
            get
            {
                string str = Read("IsAutoDeleteFile");
                if (string.IsNullOrEmpty(str) || str == "0")
                    return false;
                return true;
            }
            set
            {
                Save("IsAutoDeleteFile", value ? "1" : "0");
            }
        }

        /// <summary>
        /// 是否重复上传
        /// </summary>
        public bool RepeatUpload
        {
            get
            {
                string str = Read("RepeatUpload");
                if (string.IsNullOrEmpty(str) || str == "0")
                    return false;
                return true;
            }
            set
            {
                Save("RepeatUpload", value ? "1" : "0");
            }
        }

        #endregion 采集站文件配置

        #region 采集站

        /// <summary>
        /// 本地服务器端口
        /// </summary>
        public string LocalServerUrl
        {
            get
            {
                return Read("LocalServerUrl");
            }
            set
            {
                Save("LocalServerUrl", value);
            }
        }

        /// <summary>
        /// 服务器地址
        /// </summary>
        public string ServerUrl
        {
            get
            {
                return Read("ServerUrl");
            }
            set
            {
                Save("ServerUrl", value);
            }
        }

        /// <summary>
        /// 采集站唯一标识
        /// </summary>
        public string StationKey
        {
            get
            {
                return Read("StationKey");
            }
            set
            {
                Save("StationKey", value);
            }
        }

        /// <summary>
        /// 采集站编号
        /// </summary>
        public string StationNo
        {
            get
            {
                return Read("StationNo");
            }
            set
            {
                Save("StationNo", value);
            }
        }

        /// <summary>
        /// 采集站名称
        /// </summary>
        public string StationName
        {
            get
            {
                return Read("StationName");
            }
            set
            {
                Save("StationName", value);
            }
        }

        #endregion 采集站

        #region 保存、读取配置方法

        /// <summary>
        /// 保存配置
        /// </summary>
        /// <param name="key">key</param>
        /// <param name="value">value</param>
        public void Save(string key, string value)
        {
            try
            {
                Configuration configurationInstance = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
                configurationInstance.AppSettings.Settings[key].Value = value;
                configurationInstance.Save();
                ConfigurationManager.RefreshSection("appSettings");
            }
            catch (Exception ex)
            {
                //throw new Exception("【保存配置文件节点失败】Save：" + key, ex);
            }
        }

        /// <summary>
        /// 读取配置
        /// </summary>
        /// <param name="key">key</param>
        /// <returns>value</returns>
        public string Read(string key)
        {
            try
            {
                Configuration configurationInstance = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
                return configurationInstance.AppSettings.Settings[key].Value;
            }
            catch (Exception ex)
            {
                return string.Empty;
                //throw new Exception("【读取配置文件节点失败】Read：" + key, ex);
            }
        }

        #endregion
    }
}
